package controller;


import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Part;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import web.annotation.Method;
import web.annotation.Param;
import web.annotation.Type;

@Type("poiController")
public class PoiController {
	
	@Method(value="test",isReturnPage=true)
	public String test(@Param(value = "filepath") Part p){
		
		XSSFWorkbook wb = null;
		try {
			InputStream is = p.getInputStream();
			wb = new XSSFWorkbook(is);
			
			System.out.println(is);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "WEB-INF/page/poitest.jsp";
	}
	
	public List<Row> readExcel(XSSFWorkbook wb){
		
		List<Row> list = new ArrayList<>();
		Sheet sheet = null;
		
		int sheetCount = wb.getNumberOfSheets();
		int lastRowNum = 0;
		for(int i=0;i<sheetCount;i++){
			sheet = wb.getSheetAt(i);
			lastRowNum = sheet.getLastRowNum();
			
			for(int j=0;j<=lastRowNum;j++){
				Row row = sheet.getRow(j);
				list.add(row);
			}
		}
		
		
		return list;
	}
}
